import os

import pytest
from fastapi import HTTPException

os.environ["CRYPTID_UNIT_TEST"] = "true"
from model.user import User
from web import user


@pytest.fixture
def sample() -> User:
    return User(name="myxomop", hash="mYh0mOp")


@pytest.fixture
def fakes() -> list[User]:
    return user.get_all()


def test_create(sample):
    assert user.create(sample) == sample


def test_create_duplicate(fakes):
    with pytest.raises(HTTPException) as exc:
        user.create(fakes[0])
    assert exc.value.status_code == 409
    assert "exists" in exc.value.detail


def test_get_one(fakes):
    assert user.get_one(fakes[0].name) == fakes[0]


def test_get_one_missing():
    with pytest.raises(HTTPException) as exc:
        user.get_one("Fluffy")
    assert exc.value.status_code == 404
    assert "not found" in exc.value.detail


def test_modify(fakes):
    assert user.modify(fakes[0].name, fakes[0]) == fakes[0]


def test_modify_missing():
    with pytest.raises(HTTPException) as exc:
        user.modify("missingName", sample)
    assert exc.value.status_code == 404
    assert "not found" in exc.value.detail


def test_delete(fakes):
    assert user.delete(fakes[0].name) is None


def test_delete_missing(sample):
    with pytest.raises(HTTPException) as exc:
        user.delete("DillieWillie")
    assert exc.value.status_code == 404
    assert "not found" in exc.value.detail
